import Host from '@/host'
import router from '@/router'
import $ from "jquery"

export default {
    name: 'request',
    props: {},
    data() {
        return {
            state: 1,
            api: '',
            request_lock: true,
            auto_request: true,
            methods : 'get',
            reqData: {},
        }
    },
    computed: {
        loading() {
            return this.$store.state.loading;
        }
    },
    methods: {

        post(inter, param) {
            return this.get(inter, param, true, 'post');
        },

        /**
         * 请求
         * @param inter 接口名
         * @param param 参数
         * @param lock  请求锁防止多次请求
         */
        get(inter, param = this.reqData, lock = this.request_lock,methods = this.methods) {
            let that = this;
            return new Promise((resolve, reject) => {
                //如果请求超过1.5s开启正在加载的动画
                if (inter && inter.animation) {
                    that.$store.commit('loading', true);
                }
                $.ajax({
                    url: Host.debugHost+inter,
                    type: methods,
                    dataType: 'json',
                    data: param,
                    xhrFields: {
                        withCredentials: true
                    },
                    crossDomain: true
                }).done((result) => {
                    if (result.errCode == -4) {
                        return this.$alert("请先登录", '温馨提示', {
                            confirmButtonText: '确定',
                            beforeClose: (action, instance, done) => {
                                done();
                                router.push({
                                    path: "/login"
                                });
                            }
                        });
                    }
                    if (result.errCode == -6) {
                        this.errorCallBack(result.errCode);
                        return this.$message(result.errMsg);
                    }
                    if (result.errCode == -7) {
                        let a = [];
                        let msg = [];
                        a = result.errMsg;
                        const h = this.$createElement;
                        for (let i in a) {
                            if (a.hasOwnProperty(i)) {
                                msg.push(h('div', {
                                    style: 'color: red'
                                }, a[i]));
                            }
                        }
                        this.errorCallBack(result.errCode);
                        return this.$msgbox({
                            title: '出现错误',
                            message: h('p', null, msg),
                            confirmButtonText: '确定',
                        }).then((action, cancel) => {

                        });
                    }
                    resolve(result);
                    if (result.errCode == 0 && inter) {
                        if (inter.callback && inter.callback instanceof Function) {
                            inter.callback(result.data);
                        }
                    }
                }).fail((e) => {
                    this.$message("网络错误");
                    reject(e);
                }).always(() => {
                    console.log("请求结束");
                    if (that.$store) {
                        setTimeout(function () {
                            that.$store.commit('loading', false);
                        }, 800)
                    }
                });
            });
        },

        errorCallBack(errCode) {

        },

        reqCallBack(data, errCode, errMsg) {
            console.log(data, errCode, errMsg);
        },

        onSubmit(params = {}) {
            for (let i in params) {
                this.reqData[i] = params[i];
            }
            this.get(this.api).then(data => {
                if (data.errCode != 0) {
                    this.$message(data.errMsg);
                }
                this.reqCallBack(data.data, data.errCode, data.errMsg);
            });
        },
    },
    mounted() {

    },
    created() {

    }
}
